package com.task.controller.custom;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.task.common.annotation.Log;
import com.task.common.entity.custom.Item;
import com.task.common.entity.custom.Task;
import com.task.common.entity.custom.TaskEval;
import com.task.common.entity.custom.model.CreateTaskModel;
import com.task.common.entity.custom.model.QueryItemModel;
import com.task.common.entity.custom.model.QueryTaskModel;
import com.task.common.entity.custom.model.UpdateTaskModel;
import com.task.common.enums.BusinessType;
import com.task.common.util.body.JsonResult;
import com.task.common.util.body.ResultTool;
import com.task.common.util.model.ModelUtil;
import com.task.service.ITaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Huzz
 * @since 2022-05-21
 */
@Api(tags = "任务管理")
@RestController
@RequestMapping("/bs/task")
public class TaskController {

    @Autowired
    private ITaskService taskService;

    @ApiOperation("创建一个任务")
    @PreAuthorize("@ss.hasPermi('add:task')")
    @Log(title = "创建一个任务", businessType = BusinessType.INSERT, isSaveRequestParam = true)
    @PostMapping("/createTask")
    public JsonResult createTask(@RequestBody CreateTaskModel model) {
        boolean result = taskService.createTask(model);
        return result ? ResultTool.success() : ResultTool.fail();
    }

    @ApiOperation("任务转派")
    @PreAuthorize("@ss.hasPermi('transfer:task')")
    @Log(title = "任务转派", businessType = BusinessType.INSERT, isSaveRequestParam = true)
    @PostMapping("/taskTransfer")
    public JsonResult taskTransfer(Integer taskId, Integer oldUserId, Integer newUserId) {
        boolean result = taskService.taskTransfer(taskId, oldUserId, newUserId);
        return result ? ResultTool.success() : ResultTool.fail();
    }

    @ApiOperation("根据id编辑")
    @PreAuthorize("@ss.hasPermi('update:task')")
    @Log(title = "根据id编辑", businessType = BusinessType.UPDATE, isSaveRequestParam = true)
    @PutMapping("/updateTask/{id}")
    public JsonResult updateTask(@PathVariable("id") Integer id, UpdateTaskModel model) {
        boolean result = taskService.updateTask(id, (Task) ModelUtil.toEntityStatic(model, Task.class));
        return result ? ResultTool.success() : ResultTool.fail();
    }

    @ApiOperation("任务列表")
    @PreAuthorize("@ss.hasPermi('query:task')")
    @Log(title = "任务列表", businessType = BusinessType.INSERT, isSaveRequestParam = true)
    @GetMapping("/findTaskList")
    public JsonResult findTaskList(@RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "10") Integer pageSie,
                                   QueryTaskModel model) {
        return ResultTool.success(taskService.findTaskList(pageNum, pageSie, model));
    }

    @ApiOperation("删除一个任务")
    @PreAuthorize("@ss.hasPermi('del:task')")
    @Log(title = "删除一个任务", businessType = BusinessType.INSERT, isSaveRequestParam = true)
    @DeleteMapping("/deleteTask/{id}")
    public JsonResult deleteTask(@PathVariable("id") Integer id) {
        return taskService.deleteTask(id) ? ResultTool.success() : ResultTool.fail();
    }

    @ApiOperation("导入项目列表")
    @PreAuthorize("@ss.hasPermi('import:task')")
    @Log(title = "导入项目列表", businessType = BusinessType.IMPORT, isSaveRequestParam = true)
    @PostMapping("/importTask")
    public JsonResult importTask(@RequestParam("file") MultipartFile multipartFile) {
        return ResultTool.success(taskService.importTask(multipartFile));
    }

    @ApiOperation("导出项目列表")
    @PreAuthorize("@ss.hasPermi('export:task')")
    @Log(title = "导出项目列表", businessType = BusinessType.EXPORT, isSaveRequestParam = true)
    @PostMapping("/exportTask")
    public void exportTask(HttpServletResponse response,
                           HttpServletRequest request,
                           @RequestBody QueryTaskModel model) {
        taskService.exportTask(request, response, model);
    }

    @ApiOperation("任务评价/打分")
    @PreAuthorize("@ss.hasPermi('eval:task')")
    @Log(title = "任务评价/打分", businessType = BusinessType.INSERT, isSaveRequestParam = true)
    @GetMapping("/taskEval")
    public JsonResult taskEval(@ApiParam(name = "taskId", value = "任务id") @RequestParam Integer taskId,
                               @ApiParam(name = "userId", value = "员工id") @RequestParam Integer userId,
                               @ApiParam(name = "evaluation", value = "评价") @RequestParam String evaluation,
                               @ApiParam(name = "grade", value = "打分", allowableValues = "A,B,C,D") @RequestParam String grade) {
        return taskService.taskEval(taskId, userId, evaluation, grade) ? ResultTool.success() : ResultTool.fail();
    }

    @ApiOperation("任务评价/打分修改")
    @PreAuthorize("@ss.hasPermi('evalupdate:task')")
    @Log(title = "任务评价/打分修改", businessType = BusinessType.INSERT, isSaveRequestParam = true)
    @GetMapping("/updateEval")
    public JsonResult updateEval(@ApiParam(name = "taskId", value = "任务id") Integer taskId,
                                 @ApiParam(name = "userId", value = "员工id") Integer userId,
                                 @ApiParam(name = "evaluation", value = "评价") String evaluation,
                                 @ApiParam(name = "grade", value = "打分", allowableValues = "A,B,C,D") String grade) {
        return taskService.updateEval(taskId, userId, evaluation, grade) ? ResultTool.success() : ResultTool.fail();
    }

}
